package com.feng.behavior
{
	import com.feng.data.StateData;
	import com.feng.data.UpgradeType;
	import com.feng.debug.logger;
	import com.feng.utils.ChallengeCompute;
	import com.feng.utils.PlayerUtils;

	/**
	 * 防御行为
	 * @author 风之守望者
	 * 2013-3-28
	 */
	public class DefenseBehavior
	{
		/** 防御意志 表	值越大防御意志越强 */
		public var defenseWillTable:Array;

		private var stateData:StateData;

		public function DefenseBehavior(stateData:StateData)
		{
			this.stateData = stateData;
		}

		private function get feng():Object
		{
			return stateData.me;
		}

		/**
		 * 增加防御意志
		 * @param upgradeType 目标加成编号
		 * @param value 防御意志值
		 */
		public function addDefenseWill(upgradeType:int, value:int):void
		{
			logger("升级【" + upgradeType + "】的欲望：" + value);
			defenseWillTable[upgradeType] += value;
		}

		public function kill_steal_defense(priority:Number):void
		{
			var target:Object;

			//如我我的速度最快
			if (stateData.me.spd == stateData.speedSortList[0].spd)
			{
				//判断我是否能够拿到人头
				for each (target in stateData.enemyList)
				{
					if (stateData.me.spd - stateData.speedSortList[1].spd > 2)
					{
						if (target.hp <= 10 || target.hp <= (stateData.me.atk - target.def - 3) * 2)
						{
							return;
						}
						if (target.hp <= (stateData.me.atk + 4 - target.def) * 2)
						{
							addDefenseWill(UpgradeType.ADD_ATTACK, priority);
							return;
						}
					}
					if (target.hp <= 10)
					{
						addDefenseWill(UpgradeType.ADD_SPEED, priority);
						return;
					}
					if (stateData.me.spd - stateData.speedSortList[1].spd == 2)
					{
						if (target.hp <= (stateData.me.atk + 1 - target.def) * 2)
						{
							addDefenseWill(UpgradeType.ADD_ALL, priority);
							return;
						}
						if (target.hp <= (stateData.me.atk + 4 - target.def) * 2)
						{
							addDefenseWill(UpgradeType.ADD_ATTACK, priority);
							return;
						}
					}

					if (target.hp <= (stateData.me.atk + 4 - target.def) * 2)
					{
						addDefenseWill(UpgradeType.ADD_SPEED, priority);
						return;
					}
				}
			}

			//我有可能拿第一速度时
			if (stateData.me.spd + 2 >= stateData.speedSortList[0].spd)
			{
				for each (target in stateData.enemyList)
				{
					if (target.hp <= 10 || target.hp <= (stateData.me.atk - target.def) * 2)
					{
						addDefenseWill(UpgradeType.ADD_SPEED, priority);
						return;
					}
				}
			}

			//如果比我速度快一两点的玩家能够拿到人头
			for each (var attacker:Object in stateData.enemyList)
			{
				for each (target in stateData.enemyList)
				{
					var damage:int = attacker.atk + 4 - target.def;
					if (attacker.spd == stateData.speedSortList[0].spd)
						damage = damage * 2;
					if (target.hp <= damage)
					{
						if (stateData.me.spd + 1 == stateData.realState[attacker.id].spd)
						{
							addDefenseWill(UpgradeType.ADD_ALL, priority);
							return;
						}
						if (stateData.me.spd + 2 == stateData.realState[attacker.id].spd)
						{
							addDefenseWill(UpgradeType.ADD_SPEED, priority);
							return;
						}
					}
				}
			}

			for each (target in stateData.enemyList)
			{
				if (target.hp > stateData.me.atk - target.def - 3 && target.hp <= stateData.me.atk + 4 - target.def)
				{
					addDefenseWill(UpgradeType.ADD_ATTACK, priority);
					return;
				}
			}
		}

		public function challenge_mode_defense(priority:Number):void
		{
			//计算  我的 最优升级属性
			var bestUpgradeType:int = ChallengeCompute.getBestUpgradeType(stateData.me, stateData.enemyList[0]);
			addDefenseWill(bestUpgradeType, priority);
		}

		public function three_mode_defense(priority:Number):void
		{
			
		}

		public function four_mode_defense(priority:Number):void
		{
			if (stateData.me.def + 5 < stateData.attackSortList[1].atk)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.attackMaxSpeedNum > 0 && stateData.attackMaxSpeedNum / stateData.hasMaxSpeedNum < 1 / 3)
			{
				if (stateData.me.spd == stateData.speedSortList[0].spd + 2 //
					&& stateData.me.atk + 1 < stateData.attackedList[1].atk)
				{
					addDefenseWill(UpgradeType.ADD_ALL, priority);
					return;
				}

				if (stateData.me.spd <= stateData.speedSortList[0].spd + 2)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[1].spd)
			{
				addDefenseWill(UpgradeType.ADD_SPEED, priority);
				return;
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[0].spd)
			{
				if (stateData.speedSortList[0].hp > 100)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}

			if (stateData.me.atk + 4 < stateData.attackSortList[1].atk)
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
				return;
			}

			if (stateData.attackSortList[0].hp > 50)
			{
				if (stateData.me.atk + 4 < stateData.attackSortList[0].atk)
				{
					addDefenseWill(UpgradeType.ADD_ATTACK, priority);
					return;
				}
			}
			if (stateData.attackMaxAttackNum > 0 && stateData.attackMaxAttackNum / stateData.hasMaxAttackNum < 1 / 3)
			{
				if (stateData.me.atk + 4 <= stateData.attackSortList[0].atk)
				{
					addDefenseWill(UpgradeType.ADD_ATTACK, priority);
				}
				return;
			}

			addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
		}

		public function five_mode_defense(priority:Number):void
		{
			if (stateData.me.def + 5 < stateData.attackSortList[2].atk)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.attackMaxSpeedNum > 0 && stateData.attackMaxSpeedNum / stateData.hasMaxSpeedNum < 1 / 3)
			{
				if (stateData.me.spd == stateData.speedSortList[0].spd + 2 //
					&& stateData.me.atk + 1 < stateData.attackedList[2].atk)
				{
					addDefenseWill(UpgradeType.ADD_ALL, priority);
					return;
				}

				if (stateData.me.spd <= stateData.speedSortList[0].spd + 2)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}
			
			if (stateData.me.def + 5 < stateData.attackSortList[1].atk && stateData.me.hp <= 30)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[1].spd)
			{
				addDefenseWill(UpgradeType.ADD_SPEED, priority);
				return;
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[0].spd)
			{
				if (stateData.speedSortList[0].hp > 100)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}

			if (stateData.me.atk + 4 < stateData.attackSortList[2].atk)
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
				return;
			}

			if (stateData.attackSortList[0].hp > 80 || stateData.attackSortList[1].hp > 80)
			{
				if (stateData.me.atk + 4 < stateData.attackSortList[1].atk)
				{
					addDefenseWill(UpgradeType.ADD_ATTACK, priority);
					return;
				}
			}
			if (stateData.attackMaxAttackNum > 0 && stateData.attackMaxAttackNum / stateData.hasMaxAttackNum < 1 / 3)
			{
				if (stateData.me.atk + 4 < stateData.attackSortList[0].atk)
				{
					addDefenseWill(UpgradeType.ADD_ATTACK, priority);
				}
				return;
			}

			addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
		}

		public function six_mode_defense(priority:Number):void
		{
			if (stateData.turnIndex < 3)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.me.def + 5 < stateData.attackSortList[2].atk)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.me.def + 5 < stateData.attackSortList[1].atk && stateData.me.hp <= 50)
			{
				addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
				return;
			}

			if (stateData.attackMaxSpeedNum > 0 && stateData.attackMaxSpeedNum / stateData.hasMaxSpeedNum < 1 / 3)
			{
				if (stateData.me.spd == stateData.speedSortList[0].spd + 2 //
					&& stateData.me.atk + 1 < stateData.attackedList[2].atk)
				{
					addDefenseWill(UpgradeType.ADD_ALL, priority);
					return;
				}

				if (stateData.me.spd <= stateData.speedSortList[0].spd + 2)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[1].spd)
			{
				addDefenseWill(UpgradeType.ADD_SPEED, priority);
				return;
			}

			if (stateData.me.spd + 2 < stateData.speedSortList[0].spd)
			{
				if (stateData.speedSortList[0].hp > 100)
				{
					addDefenseWill(UpgradeType.ADD_SPEED, priority);
					return;
				}
			}

			if (stateData.me.atk + 4 < stateData.attackSortList[2].atk)
			{
				addDefenseWill(UpgradeType.ADD_ATTACK, priority);
				return;
			}

			addDefenseWill(UpgradeType.ADD_DEFENSE, priority);
		}

	}
}
